//---------------------------------------------------------------------------
// MyStatement Class
// Author : Alex Woo-Hyun Kim at www.webrobot.org
// Email : woorung@webrobot.org,webrobot@hotmail.com,woorung@empal.com
// Date : May 6, 2002
//---------------------------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include <strings>
#include <string>
#include "CMyStatement.h"
#include "CStringTokenizer.h"
//---------------------------------------------------------------------------
CMyStatement::CMyStatement(char *pszStmt)
{
	m_pszFinalStmt = NULL;

	CStringTokenizer *pToken = new CStringTokenizer(pszStmt,"?");

	m_iCount = pToken->GetTotalCount();
	if(pszStmt[strlen(pszStmt)-1]=='?') m_iIsMore = 0;
	else m_iIsMore = -1;
	m_iCount = (m_iCount*2)+m_iIsMore;

	m_ppszStmt = new char *[m_iCount];
	
	int iCurCount = 0;
	char *pszItem;
	while(pToken->HasMoreElements()) {
		pszItem = pToken->GetNextElement();
		m_ppszStmt[iCurCount] = new char[strlen(pszItem)+1];
		strcpy(m_ppszStmt[iCurCount],pszItem);

		if(iCurCount+1<m_iCount) m_ppszStmt[iCurCount+1] = NULL;
		iCurCount+=2;
	}

	delete pToken;
}
//--------------------------------------------------------------------------
CMyStatement::CMyStatement(char *pszStmt,char *pszPattern)
{
	m_iCount = 0;
	m_pszFinalStmt = NULL;
		
	int len = strlen(pszStmt);
	int patlen = strlen(pszPattern);
	char *pszTmp = new char[len+1];
	strcpy(pszTmp,pszStmt);

	char *pszTmpPos = pszTmp;
	char *pszStartPos = pszTmpPos;
	while((pszTmpPos=strstr(pszTmpPos,pszPattern))!=NULL) {
		m_iCount++;
		*pszTmpPos = '\0';
		if(*(pszTmpPos+patlen)=='\0') {
			break;
		}
		pszTmpPos+=patlen;
		pszStartPos = pszTmpPos;
	}
	if(pszTmpPos==NULL) m_iIsMore = 1;
	else m_iIsMore = 0;

	m_iCount = (m_iCount*2)+m_iIsMore;
	m_ppszStmt = new char *[m_iCount];

	pszStartPos = pszTmp;
	for(int i=0;i<m_iCount;i+=2) {
		len = strlen(pszStartPos);
		m_ppszStmt[i] = new char[len+1];
		strcpy(m_ppszStmt[i],pszStartPos);
		if(i+1<m_iCount) m_ppszStmt[i+1] = NULL;
		pszStartPos = pszStartPos + len + patlen;
	}
	
	delete []pszTmp;
}
//--------------------------------------------------------------------------
CMyStatement::~CMyStatement()
{
	if(m_pszFinalStmt!=NULL) delete []m_pszFinalStmt;

	for(int i=0;i<m_iCount;i++) {
		if(m_ppszStmt[i]!=NULL) delete []m_ppszStmt[i];
		//delete m_ppszStmt[i];
	}
	delete []m_ppszStmt;
	//delete m_ppszStmt;
}
//--------------------------------------------------------------------------
void CMyStatement::SetName(int iNamePos,char *pszName)
{
	if(iNamePos<0) return;
	if(pszName==NULL||!strcmp(pszName,"")) {
		for(int i=1,k=1;i<m_iCount;i+=2,k++) {
                	if(k==iNamePos&&m_ppszStmt[i]!=NULL) {
                        	delete []m_ppszStmt[i];
                        	m_ppszStmt[i] = NULL;
				break;
                	}
		}
                return;
        }


	int len = strlen(pszName);
	char *pszItem = new char[len+1];
	strcpy(pszItem,pszName);

	int count = 0;
	char *pszTmp = pszItem;
	while((pszTmp=strchr(pszTmp,'\''))!=NULL) {
		pszTmp++;
		count++;
	}
	pszTmp = pszItem;
	while((pszTmp=strchr(pszTmp,'\"'))!=NULL) {
		pszTmp++;
		count++;
	}

	for(int i=1,k=1;i<m_iCount;i+=2,k++) {
		if(k==iNamePos) {
			if(m_ppszStmt[i]!=NULL) {
				delete []m_ppszStmt[i];
				m_ppszStmt[i] = NULL;
			}
			m_ppszStmt[i] = new char[len+1+count];
			int k,m;
			for(k=0,m=0;*(pszItem+k);k++) {
				if(pszItem[k]=='\''||pszItem[k]=='\"') {
					m_ppszStmt[i][m++] = '\\';
				}
				m_ppszStmt[i][m++] = pszItem[k];
			}
			m_ppszStmt[i][m] = '\0';
			break;
		}	
	}

	delete []pszItem;
}
//--------------------------------------------------------------------------
void CMyStatement::SetString(int iNamePos,char *pszValue)
{
	if(iNamePos<0) return;
	if(pszValue==NULL||strcmp(pszValue,"")==0) {
                for(int i=1,k=1;i<m_iCount;i+=2,k++) {
                        if(k==iNamePos&&m_ppszStmt[i]!=NULL) {
                                delete []m_ppszStmt[i];
                                m_ppszStmt[i] = NULL;
                                break;
                        }
                }
                return;
        }

	int len = strlen(pszValue);
	char *pszItem = new char[len+1];
	strcpy(pszItem,pszValue);

	int count = 0;
	char *pszTmp = pszItem;
        while((pszTmp=strchr(pszTmp,'\''))!=NULL) {
		pszTmp++;
		count++;
        }
	pszTmp = pszItem;
        while((pszTmp=strchr(pszTmp,'\"'))!=NULL) {
                pszTmp++;
                count++;
        }


        for(int i=1,k=1;i<m_iCount;i+=2,k++) {
                if(k==iNamePos) {       
			if(m_ppszStmt[i]!=NULL) {
				delete []m_ppszStmt[i];
				m_ppszStmt[i] = NULL;
			}	
                        m_ppszStmt[i] = new char[len+3+count];
			int k,m;
			m_ppszStmt[i][0] = '\'';
			for(k=0,m=1;*(pszItem+k);k++) {
				if(pszItem[k]=='\''||pszItem[k]=='\"') {
					m_ppszStmt[i][m++] = '\\';
				}
				m_ppszStmt[i][m++] = pszItem[k];
			}
			m_ppszStmt[i][m++] = '\'';
			m_ppszStmt[i][m] = '\0';
			break;
                }       
        }

	delete []pszItem;
}
//--------------------------------------------------------------------------
void CMyStatement::SetInteger(int iNamePos,int iValue)
{
	if(iNamePos<0) return;

        for(int i=1,k=1;i<m_iCount;i+=2,k++) {
                if(k==iNamePos) {       
			if(m_ppszStmt[i]!=NULL) {
				delete []m_ppszStmt[i];
				m_ppszStmt[i] = NULL;
			}
                        m_ppszStmt[i] = new char[50];
			sprintf(m_ppszStmt[i],"%d",iValue);
                        break;
                }       
        }
}
//--------------------------------------------------------------------------
char *CMyStatement::GetStatement()
{
	int iTotalCount = 0;
	for(int i=0;i<m_iCount;i++) {
		if(m_ppszStmt[i]==NULL) iTotalCount += 4;
		else iTotalCount += strlen(m_ppszStmt[i]);
	}

	if(m_pszFinalStmt!=NULL) {
		delete []m_pszFinalStmt;
		m_pszFinalStmt = NULL;
	}

	m_pszFinalStmt = new char[iTotalCount+1];

	strcpy(m_pszFinalStmt,m_ppszStmt[0]);
	for(int i=1;i<m_iCount;i++) {
		//if(m_ppszStmt[i]==NULL) strcat(m_pszFinalStmt,"NULL");
		if(m_ppszStmt[i]==NULL||!strcmp(m_ppszStmt[i],"NULL")) continue;
		else strcat(m_pszFinalStmt,m_ppszStmt[i]);
	}

	return m_pszFinalStmt;
}
//--------------------------------------------------------------------------
